package org.gl.rtsp;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
/***
 * 标准的URI解析
 * @author GeLiang
 *
 */
public class URIParse {
	private Map<String, String> paramMap = new HashMap<String, String>();
	private String host;
	private int port;
	private String uri;
	private String protocolName;

	public static void main(String[] args) throws URISyntaxException {

		String u = "rtsp://proxyip:213333?c=rtsp://carmeip:321633/fas321dfas/f123asdfasf&w=GUID";
		URIParse uri = new URIParse(u);
		System.out.println(uri.getHost());
		System.out.println(uri.getPort());
		System.out.println(uri.getProtocolName());
		System.out.println(uri.getUri());
		System.out.println(uri.query("c"));
		System.out.println(uri.query("w"));
		System.out.println(uri.query("a"));
	}

	private String query(String key) {
		return paramMap.get(key);
	}

	public URIParse(String url) throws URISyntaxException {
		URI uri = new URI(url);
		host = uri.getHost();
		port = uri.getPort();
		protocolName = uri.getScheme();
		this.uri = uri.getQuery();
		if (this.uri != null) {
			String paramaters[] = this.uri.split("&");
			for (String param : paramaters) {
				String values[] = param.split("=");
				paramMap.put(values[0], values[1]);
			}
		}

	}

	public Map<String, String> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, String> paramMap) {
		this.paramMap = paramMap;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public String getProtocolName() {
		return protocolName;
	}

	public void setProtocolName(String protocolName) {
		this.protocolName = protocolName;
	}

}